package com.example.administrator.dc.adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.dc.Bean.Checkcollect;
import com.example.administrator.dc.Bean.Collectlistresult;
import com.example.administrator.dc.Bean.DataEntity;
import com.example.administrator.dc.R;
import com.example.administrator.dc.Service.CollectListener;
import com.example.administrator.dc.Service.CollectService;
import com.example.administrator.dc.model.CollectModel;
import com.example.administrator.dc.model.Contants;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/8/24.
 */

public class CollectlistAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements CollectListener{

    private Context context;
    private LayoutInflater layoutInflater;
    private List<DataEntity> collectlist;
    public CollectlistAdapter(Context cxt,List<DataEntity> collect){
        this.context =cxt;
        this.collectlist=collect;
        layoutInflater=layoutInflater.from(cxt);
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView=layoutInflater.inflate(R.layout.cardview_collect,parent,false);
        ViewHolder viewHolder=new ViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof  ViewHolder){
           final ViewHolder viewHolder=(ViewHolder) holder;
            final DataEntity cook=collectlist.get(position);
            viewHolder.user_info.setText(cook.getTitle());
            viewHolder.user_read.setText(cook.getRead()+"");
            viewHolder.user_collect.setText(cook.getLove());
            SharedPreferences sp = context.getSharedPreferences("UserInfo", context.MODE_PRIVATE);
            final String userid =sp.getString("user_id", null);
            if(!"".equals(userid)){
                        Retrofit retrofit=new Retrofit.Builder().baseUrl(Contants.BASE_URL1).
                                addConverterFactory(GsonConverterFactory.create()).build();
                        CollectService service=retrofit.create(CollectService.class);
                        Call<Checkcollect> call= service.checkcollect(userid,cook.getBid());
                        call.enqueue(new Callback<Checkcollect>() {
                            @Override
                            public void onResponse(Call<Checkcollect> call, Response<Checkcollect> response) {
                                if(response.body().getStatus()==1){
                                    viewHolder.imageView_collect1.setImageDrawable(context.getDrawable(R.drawable.redheart));
                                }
                                else {
                                    viewHolder.imageView_collect1.setImageDrawable(context.getDrawable(R.drawable.heart));
                                }

                            }

                    @Override
                    public void onFailure(Call<Checkcollect> call, Throwable t) {

                    }
                });


            }
            if (!"".equals(cook.getPhoto()))
                Picasso.with(context).load(cook.getPhoto()).into(viewHolder.user_image);
            if(!"1".equals(cook.getGender()+"")){
                viewHolder.user_bottom.setBackgroundResource(R.color.myhome_top_text_color);
                viewHolder.user_info.setTextColor(viewHolder.user_info.getResources().getColor(R.color.myhome_top_text_color));}
            else
            {viewHolder.user_bottom.setBackgroundResource(R.color.pink);
                viewHolder.user_info.setTextColor(viewHolder.user_info.getResources().getColor(R.color.pink));}
            viewHolder.imageView_collect1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    SharedPreferences sp = context.getSharedPreferences("UserInfo", context.MODE_PRIVATE);
                    final String userid =sp.getString("user_id", null);
                    Retrofit retrofit=new Retrofit.Builder().baseUrl(Contants.BASE_URL1).
                            addConverterFactory(GsonConverterFactory.create()).build();
                    CollectService service=retrofit.create(CollectService.class);
                    Call<Checkcollect> call= service.checkcollect(userid,cook.getBid());
                    call.enqueue(new Callback<Checkcollect>() {
                        @Override
                        public void onResponse(Call<Checkcollect> call, Response<Checkcollect> response) {
                            if(response.body().getStatus()==1){
                                CollectModel collectModel=new CollectModel();
                                collectModel.uncollect(userid,cook.getBid(),CollectlistAdapter.this);
                                viewHolder.imageView_collect1.setImageDrawable(context.getDrawable(R.drawable.heart));
                                Toast.makeText(context.getApplicationContext(),"取消收藏成功",Toast.LENGTH_SHORT).show();

                            }
                            else {
                                CollectModel collectModel=new CollectModel();
                                collectModel.collect(userid,cook.getBid(),CollectlistAdapter.this);
                                viewHolder.imageView_collect1.setImageDrawable(context.getDrawable(R.drawable.redheart));
                                Toast.makeText(context.getApplicationContext(),"收藏成功",Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onFailure(Call<Checkcollect> call, Throwable t) {

                        }
                    });

                }
            });





        }





    }

    @Override
    public int getItemCount() {
        return collectlist==null ? 0:collectlist.size();
    }

    @Override
    public void onResponse(String msg) {

    }

    @Override
    public void onFail(String s) {

    }

    @Override
    public void onExitResponse(String msg) {

    }

    @Override
    public void onExitFail(String s) {

    }

    @Override
    public void onFailResponse(String msg) {

    }

    @Override
    public void onResponse1(String msg) {

    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView user_info;
        ImageView user_image;
        TextView user_read;
        TextView user_collect;
        ImageView imageView_collect1;
        RelativeLayout user_bottom;

        public ViewHolder(View itemView) {
            super(itemView);
         //   imageView_collect1=itemView.findViewById(R.id.imageView_collect);
            user_info = (TextView) itemView.findViewById(R.id.cardview_myhome_info);
            user_image = (ImageView) itemView.findViewById(R.id.cardview_myhome_img);
            user_read = (TextView) itemView.findViewById(R.id.num_myhome_read);
            user_collect= (TextView) itemView.findViewById(R.id.num_myhome_collect);
            user_bottom=(RelativeLayout)itemView.findViewById(R.id.cardview_bottom);

        }
    }
}
